////////////////////////////////////////////////////////////
// Workfile : Crypter.cpp
// Author : Andreas Lehner
// Date : 12.11.2012
// Description : class Crypter: implementation of Adaptee class (intern interface)
// Remarks : -
// Revision : 1
////////////////////////////////////////////////////////////
#include <fstream>
#include <sstream>
#include "Crypter.h"

bool Crypter::ReadFile(std::string const& fileName){
	//open the file and read data binary -> when reading-type is default,
	//ascii-code 21 (eof) leads to stop reading ex.
	std::ifstream infile(fileName, std::ios::binary);
	//check if file is open
	if (infile.is_open()){
		//store every character of the input file in a std::string
		std::string toCrypt((std::istreambuf_iterator<char>(infile)), std::istreambuf_iterator<char>());
		mData = toCrypt;
		//close file
		infile.close();
		return true;
	}
	return false;
}

bool Crypter::WriteFile(std::string const& fileName){
	//open the file and write data binary
	std::ofstream outfile(fileName, std::ios::binary);
	//check if file is open
	if(outfile.is_open()){
		//write data to output file
		outfile << mData;
		//close file
		outfile.close();
		return true;
	}
	return false;
}

std::string Crypter::getRawFilename(std::string const& fileName) const{
	//filename is empty -> throw exception
	if (fileName.empty()) {
		throw "getRawFilename: Please specifiy a valid filename";
	}
	//find file extension
	size_t end = fileName.rfind(getFileExtension());
	end = (end != fileName.npos ? end : fileName.size());
	//return filename without extension
	return fileName.substr(0, end);
}

std::string Crypter::getOutputFilename(std::string const& fileName) const{
	//filename is empty -> throw exception
	if (fileName.empty()) {
		throw "getOutputFilename: Please specifiy a valid filename";
	}

	std::string ret (fileName);
	//append file extension to filename and return it
	return ret.append(getFileExtension());
}